'''
Created on 2017年3月16日

@author: Administrator
'''
import pymysql
from pymysql.err import MySQLError
connection = pymysql.connect('localhost', 'root', '', 'test')

#单个查询
def query_signle(idNo,connection):
    try:
        #打开游标
        with connection.cursor() as cursor:
            # Read a single record
            sql = "SELECT * FROM `user` WHERE `id_no`=%s"
            #执行SQL操作
            cursor.execute(sql, (idNo))
            result = cursor.fetchone()
            return result
    finally:
        connection.close()
        
#查询所有       
def queryAll(connection):
    try:
        #打开游标
        with connection.cursor() as cursor:
            # Read a single record
            sql_all = "SELECT * FROM `user`"
            #执行SQL操作
            cursor.execute(sql_all)
            #查询全部(集合)
            result = cursor.fetchall()
            return result
    except MySQLError: 
        print("error")
        #错误回滚
        connection.rollback()
    
           
all_users = queryAll(connection)
print("查询到的所有用户为：",list(all_users))
idNo = '11111'
user = query_signle(idNo,connection)
print("身份证号为"+idNo+"的用户为：",user)
